﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Net;

public partial class RequestReview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
            //if don't have rqid param return and display message.
            if (Request.Params.Get("rqid") == null)
            {
                this.btnFinish.Enabled = false;
                return;
            }
            String strRqCode = Request.Params.Get("rqid");
            LoadDataField(strRqCode);
            LoadGridView(strRqCode);
            SetHeaderText();
        }
        else
        {

        }
    }

    protected void btnFinish_Click(object sender, EventArgs e)
    {
        ProRequest pr = new ProRequest();
        String strPIC = "";
        String strDirector = "";
        String strVPre = "";

        DataSet ds = new DataSet();
        DataSet ds2 = new DataSet();
        DataSet ds3 = new DataSet();
        DataSet ds4 = new DataSet();
        ds = pr.LoadRequestInfo(Request.Params.Get("rqid"));

        ds2 = new DataSet();
        ds2 = pr.LoadPic(ds.Tables[0].Rows[0]["pic"].ToString());
        strPIC = ds2.Tables[0].Rows[0]["username"].ToString();

        ds3 = new DataSet();
        ds3 = pr.LoadDirector(ds.Tables[0].Rows[0]["director"].ToString());
        strDirector = ds3.Tables[0].Rows[0]["username"].ToString();

        ds4 = new DataSet();
        ds4 = pr.LoadVPre(ds.Tables[0].Rows[0]["vpresident"].ToString());
        strVPre = ds4.Tables[0].Rows[0]["username"].ToString();

        Boolean bResult = false;
        ds = new DataSet();
        ds = pr.LoadRequestConfirmInfo(Request.Params.Get("rqid"));

        //Create new
        if (ds == null || ds.Tables[0].Rows.Count == 0)
        {
            //create 3 rows and insert to REQUEST_COFIRM table for PIC, Director, VPresident.
            bResult = pr.CreateConfirmList(Request.Params.Get("rqid"), strPIC, strDirector, strVPre);
            if (bResult == false)
            {
                //display error message
                return;
            }
            else
            {
                //create email and sent to: submitter, pic, director, vpresident
                SentToSubmitter(((UserInfo)Session["userinfo"]).StrEmail, "Request Form Registered and Sent to Manager", Request.Params.Get("rqid"));
                SentToManager(ds2.Tables[0].Rows[0]["email"].ToString(), ds2.Tables[0].Rows[0]["username"].ToString(), Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
                //SentToManager(ds3.Tables[0].Rows[0]["email"].ToString(), ds3.Tables[0].Rows[0]["username"].ToString(), Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
                //SentToManager(ds4.Tables[0].Rows[0]["email"].ToString(), ds4.Tables[0].Rows[0]["username"].ToString(), Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
            }
        }
        // update
        else if (ds != null &&  (ds.Tables[0].Rows[0]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                            ds.Tables[0].Rows[1]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString() ||
                                            ds.Tables[0].Rows[2]["result"].ToString() == Constant.REQUEST_STATUS_REJECT.ToString()))
        {
            bResult = pr.UpdateConfirmList(Request.Params.Get("rqid"));
            if (bResult == false)
            {
                //display error message
                return;
            }
            else
            {
                //create email and sent to: submitter, pic, director, vpresident
                SentToSubmitter(((UserInfo)Session["userinfo"]).StrEmail, "Request Form Registered and Sent to Manager", Request.Params.Get("rqid"));
                SentToManager(ds2.Tables[0].Rows[0]["email"].ToString(), ds2.Tables[0].Rows[0]["username"].ToString(), Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
                //SentToManager(ds3.Tables[0].Rows[0]["email"].ToString(), ds3.Tables[0].Rows[0]["username"].ToString(), Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
                //SentToManager(ds4.Tables[0].Rows[0]["email"].ToString(), ds4.Tables[0].Rows[0]["username"].ToString(), Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
            }
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
        Response.Redirect("Default.aspx");
    }

    public void SentToManager(String strMailTo, String strPIC, String strSubmitter, String strSubject, String strRqid)
    {
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress(Constant.EMAIL_ADMIN, Constant.EMAIL_DISPLAY_ADMIN);
        mm.To.Add(new MailAddress(strMailTo));
        mm.Subject = strSubject;
        mm.IsBodyHtml = true;
        mm.Body =
      "<div  style=\"width:600px; background-color:#fff; margin:0 auto; float:left; margin-left:10px;text-align:justify;\">" +
      "<img src=\"images/logo copy.jpg\" style=\"margin:10px 0;\" /><br />" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Hi <strong>" + strPIC + "</strong>!</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">You have <strong>request</strong> from <strong>" + strSubmitter + "<strong>.</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Please click below link to view all request waitting to review and process:</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\"><a href=\"http://techdata.com.vn/requestlist.aspx?cfb=" + strPIC + "\">http://techdata.com.vn/requestlist.aspx?cfb=" + strPIC + "</a></p>" +
      "<p>-----------------------------------------------------------------------</p>" +
      "<p>If you have more requires, please contact Techdata Support Team to get more information.</p>" +
      "<p><span class=\"style1\">Techdata Administrator Team</span><br />" +
      "Email: <a href=\"mailto:info@techdata.com.vn\">info@techdata.com.vn<</a><br />" +
      "Phone: 0907246145<br />" +
      "  Website: <a href=\"http://techdata.com.vn\">techdata.com.vn</a></p>" +
      "<p>      Thanks for use our service!</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">&nbsp;</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; float:left;\">&nbsp;</p>" +
      "<br />" +
      "</div>";

        SmtpClient sc = new SmtpClient();
        sc.Host = "smtp.gmail.com";
        sc.Port = 587;
        sc.Credentials = new NetworkCredential(Constant.EMAIL_ADMIN, Constant.PASS_EMAIL_ADMIN);
        sc.EnableSsl = true;
        sc.DeliveryMethod = SmtpDeliveryMethod.Network;
        sc.Send(mm);
    }

    public void SentToSubmitter(String strToMail, String strSubject, String strRqid)
    {
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress(Constant.EMAIL_ADMIN, Constant.EMAIL_DISPLAY_ADMIN);
        mm.To.Add(new MailAddress(strToMail));
        mm.Subject = strSubject;
        mm.IsBodyHtml = true;
        mm.Body = "<div  style=\"width:600px; background-color:#fff; margin:0 auto; float:left; margin-left:10px;text-align:justify;\">" +
        "<img src=\"images/logo copy.jpg\" style=\"margin:10px 0;\" /><br />" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Hi <strong>" + Session["user"].ToString() + "</strong>!</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">You have just send 1 request form system. Please wait for accept or reject from PIC, Director and VPresident</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Please click below link to review or edit your request:</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\"><a href=\"http://techdata.com.vn/requestform.aspx?rqid=" + strRqid + "\">http://techdata.com.vn/requestform.aspx?rqid=" + strRqid + "</a></p>" +
        "<p>-----------------------------------------------------------------------</p>" +
        "<p>If you have more requires, please contact Techdata Support Team to get more information.</p>" +
        "<p><span class=\"style1\">Techdata Administrator Team</span><br />" +
        "Email: <a href=\"mailto:info@techdata.com.vn\">info@techdata.com.vn</a><br />" +
        "Phone: 0907246145<br />" +
        "  Website: <a href=\"http://techdata.com.vn\">techdata.com.vn</a></p>" +
        "<p>      Thanks for use our service!</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">&nbsp;</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; float:left;\">&nbsp;</p>" +
        "<br />" +
        "</div>";

        SmtpClient sc = new SmtpClient();
        sc.Host = "smtp.gmail.com";
        sc.Port = 587;
        sc.Credentials = new NetworkCredential(Constant.EMAIL_ADMIN, Constant.PASS_EMAIL_ADMIN);
        sc.EnableSsl = true;
        sc.DeliveryMethod = SmtpDeliveryMethod.Network;
        sc.Send(mm);
    }
    private void LoadDataField(String strRqCode)
    {
        ProRequest pr = new ProRequest();
        DataSet ds = new DataSet();
        ds = pr.LoadRequestInfo(strRqCode);
        this.lblRequestCode.Text = ds.Tables[0].Rows[0]["code"].ToString();
        this.lblDateRequest.Text = DateTime.Parse(ds.Tables[0].Rows[0]["date"].ToString()).ToShortDateString();
        this.lblRemark.Text = ds.Tables[0].Rows[0]["remark"].ToString();
        this.lblAmount.Text = ds.Tables[0].Rows[0]["amount"].ToString();
        this.lblDescription.Text = ds.Tables[0].Rows[0]["description"].ToString();
        this.lblSubmitter.Text = ds.Tables[0].Rows[0]["username"].ToString();

        DataSet ds2 = pr.LoadBusCat(ds.Tables[0].Rows[0]["division"].ToString());
        this.lblProjectDiv.Text = ds2.Tables[0].Rows[0]["catalog_name"].ToString();

        ds2 = new DataSet();
        ds2 = pr.LoadListPartner(ds.Tables[0].Rows[0]["bp_user"].ToString());
        this.lblUserBP.Text = ds2.Tables[0].Rows[0]["company"].ToString();

        ds2 = new DataSet();
        ds2 = pr.LoadPic(ds.Tables[0].Rows[0]["pic"].ToString());
        this.lblPicName.Text = ds2.Tables[0].Rows[0]["fullname"].ToString();
        this.imgPicSign.ImageUrl = ds2.Tables[0].Rows[0]["scan_signature"].ToString();

        ds2 = new DataSet();
        ds2 = pr.LoadDirector(ds.Tables[0].Rows[0]["director"].ToString());
        this.lblDriectorName.Text = ds2.Tables[0].Rows[0]["fullname"].ToString();
        this.imgDirectorSign.ImageUrl = ds2.Tables[0].Rows[0]["scan_signature"].ToString();

        ds2 = new DataSet();
        ds2 = pr.LoadVPre(ds.Tables[0].Rows[0]["vpresident"].ToString());
        this.lblVPreName.Text = ds2.Tables[0].Rows[0]["fullname"].ToString();
        this.imgVPreSign.ImageUrl = ds2.Tables[0].Rows[0]["Scan_signature"].ToString();
    }

    private void LoadGridView(String strRqCode)
    {
        ProRequest pr = new ProRequest();
        DataSet ds = new DataSet();
        ds = pr.LoadRequestDetail(strRqCode);
        this.grdRequestDetail.DataSource = ds.Tables[0];
        this.grdRequestDetail.DataBind();
    }

    private void SetHeaderText()
    {
        this.grdRequestDetail.HeaderRow.Cells[0].Text = "Request Code";
        this.grdRequestDetail.HeaderRow.Cells[0].Width = 200;
        this.grdRequestDetail.HeaderRow.Cells[1].Text = "ID";
        this.grdRequestDetail.HeaderRow.Cells[2].Text = "Date";
        this.grdRequestDetail.HeaderRow.Cells[3].Text = "Voucher";
        this.grdRequestDetail.HeaderRow.Cells[4].Text = "Description";
        this.grdRequestDetail.HeaderRow.Cells[5].Text = "Quantity";
        this.grdRequestDetail.HeaderRow.Cells[6].Text = "Amount(USD)";
        this.grdRequestDetail.HeaderRow.Cells[7].Text = "Ex.Rate";
        this.grdRequestDetail.HeaderRow.Cells[8].Text = "Amount(VND)";
        this.grdRequestDetail.HeaderRow.Cells[9].Text = "%VAT";
        this.grdRequestDetail.HeaderRow.Cells[10].Text = "Amount(VAT)";
        this.grdRequestDetail.HeaderRow.Cells[11].Text = "Total";
    }
}
